<template>
	<el-dialog v-model="visible" :title="title" :close-on-click-modal="false">
		<!-- <template #body> -->
		<el-form :disabled="formDisabled" ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="120px" @keyup.enter="submitHandle()">
			<el-form-item prop="groupName" label="调度组名称">
				<el-input v-model="dataForm.groupName" placeholder="请输入调度组名称"></el-input>
			</el-form-item>
			<el-form-item prop="groupOwner" label="调度组负责人">
				<el-input v-model="dataForm.groupOwner" placeholder="请输入调度组负责人"></el-input>
			</el-form-item>
			<el-form-item prop="phone" label="负责人电话">
				<el-input v-model="dataForm.phone" placeholder="请输入负责人电话"></el-input>
			</el-form-item>
			<el-form-item prop="groupDesc" label="调度组描述">
				<el-input v-model="dataForm.groupDesc" :rows="4" type="textarea" placeholder="请输入调度组描述" />
			</el-form-item>
		</el-form>
		<!-- </template> -->
		<template #footer v-if="!formDisabled">
			<el-button @click="visible = false">取消</el-button>
			<el-button :loading="loading" type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import store from '@/store'
import { useCrud } from '@/hooks'
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { IHooksOptions } from '@/hooks/interface'

import { schedulerApi, schedulerGroupPostSubmit } from '@/api/task-scheduling'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const postList = ref<any[]>([])
const roleList = ref<any[]>([])
const orgList = ref([])
const dataFormRef = ref()

const dataForm = reactive({
	id: '',
	groupName: '',
	groupOwner: '',
	phone: '',
	groupDesc: ''
})

const loading = ref(false)

const formDisabled = ref(false)

const title = ref('')

const init = (id?: number, type?: string) => {
	// console.log(store.userStore.user.orgId)
	visible.value = true
	dataForm.id = ''

	if (type === 'details') {
		formDisabled.value = true
		title.value = '详情'
	} else {
		formDisabled.value = false
		title.value = '编辑'
	}

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	// id 存在则为修改
	if (id) {
		getScheduler(id)
	} else {
		title.value = '新建'
	}
}

const getScheduler = id => {
	schedulerApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({
	groupName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	groupOwner: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

// 表单提交
const submitHandle = () => {
	loading.value = true
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			loading.value = false
			return false
		}

		const data = {
			publishStatus: '0',
			publishTime: '2023-11-11 00:00:00',
			publisher: '100'
		}

		Object.assign(data, dataForm)

		schedulerGroupPostSubmit(data).then(res => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					loading.value = false
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
